﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataWorks_Butterworth
{
    public static class ButterworthFilterMethod
    {
        public static double[] ExcuteButterworthFilter1D( this double[] inputdata, ButterworthParaModal modal)
        {
            if (modal.usefilter==1)
            {
                double[] result = new double[inputdata.Length];
                IIR_Butterworth_Interface IBI = new DataWorks_Butterworth.IIR_Butterworth_Implementation();
                double[][] coeff_final = new double[2][];
                int coeff_numb = 0;
                double Nyquist_F = modal.samplerate / 2;
                switch (modal.type_filt)
                {
                    case 0:
                        coeff_numb = 2 * modal.order + 1;
                        for (int i = 0; i < 2; i++)
                        {
                            coeff_final[i] = new double[coeff_numb];
                        }
                        coeff_final = IBI.Lp2bp(modal.highpass / Nyquist_F, modal.lowpass / Nyquist_F, modal.order);
                        result = IBI.Filter_Data(coeff_final, inputdata);
                        break;
                    case 1:
                        coeff_numb = 2 * modal.order + 1;
                        for (int i = 0; i < 2; i++)
                        {
                            coeff_final[i] = new double[coeff_numb];
                        }
                        coeff_final = IBI.Lp2bs(modal.highpass / Nyquist_F, modal.lowpass / Nyquist_F, modal.order);
                        var outdata = IBI.Filter_Data(coeff_final, inputdata);
                        break;
                    case 2:
                        coeff_numb = modal.order + 1;
                        for (int i = 0; i < 2; i++)
                        {
                            coeff_final[i] = new double[coeff_numb];
                        }
                        coeff_final = IBI.Lp2hp(modal.highpass / Nyquist_F, modal.order);
                        result = IBI.Filter_Data(coeff_final, inputdata);
                        break;
                    case 3:
                        coeff_numb = modal.order + 1;
                        for (int i = 0; i < 2; i++)
                        {
                            coeff_final[i] = new double[coeff_numb];
                        }
                        coeff_final = IBI.Lp2lp(modal.highpass / Nyquist_F, modal.order);
                        result = IBI.Filter_Data(coeff_final, inputdata);
                        break;
                }
                return result;
            }
            else
            {
                return inputdata;
            }
        }
        public static double[][] ExcuteButterworthFilter2DArray(this double[][] inputdata, ButterworthParaModal modal)
        {
            double[][] result = new double[inputdata.Length][];
            for (int i = 0; i < inputdata.Length; i++)
            {
                result[i] = inputdata[i].ExcuteButterworthFilter1D(modal);
            }
            return result;
        }
        public static List<double[]> ExcuteButterworthFilter2DList(this List<double[]> inputdata, ButterworthParaModal modal)
        {
            List<double[]> result = new List<double[]>();
            foreach (var arr in inputdata)
            {
                result.Add(arr.ExcuteButterworthFilter1D(modal));
            }
            return result;
        }
    }
}
